package com.exam.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.demo.common.Result;
import com.exam.demo.controller.dto.QuestionDTO;
import com.exam.demo.entity.Answer;
import com.exam.demo.entity.ExamInfo;
import com.exam.demo.entity.Question;
import com.exam.demo.entity.Student;
import com.exam.demo.service.AnswerService;
import com.exam.demo.service.ExamInfoService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 功能描述
 *
 * @author yxj30
 * @date 2022/03/16  19:18
 */
@RestController
@RequestMapping("/examInfo")
public class ExamInfoController {
    @Resource
    ExamInfoService examInfoService;

    @Resource
    AnswerService answerService;
    //查找页面数据
    @GetMapping
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String examInfoName) {

        QueryWrapper<ExamInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("examInfo_name", examInfoName);
        Page<ExamInfo> page = examInfoService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }

    //查找根据id全部专业数据
    @GetMapping("/selectByCollegeId/{college_id}")
    public Result findPage(@PathVariable String college_id) {
        QueryWrapper<ExamInfo> queryWrapper = new QueryWrapper<>();
        List<ExamInfo> list = examInfoService.list(queryWrapper.eq("college_id", college_id));
        return Result.success(list);
    }


    //更改和插入
    @PostMapping("/save")
    public Result insert(@RequestBody ExamInfo examInfo) {
        return Result.success(examInfoService.saveOrUpdate(examInfo));
    }

    //删除
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(examInfoService.removeById(id));
    }

    //查看成绩
    @GetMapping("/selectScore")
    public Result selectScore(@RequestParam Integer pageNum,
                              @RequestParam Integer pageSize,
                              @RequestParam(defaultValue = "") String paperName,
                              @RequestParam(defaultValue = "") String studentId) {

        Page<ExamInfo> page = examInfoService.selectScore(new Page<>(pageNum, pageSize), paperName, studentId);
        return Result.success(page);
    }

    //批改试卷
    @GetMapping("/checkPaper")
    public Result checkPaper(@RequestParam Integer pageNum,
                             @RequestParam Integer pageSize,
                             @RequestParam(defaultValue = "-1") Integer status,
                             @RequestParam(defaultValue = "") String paperName,
                             @RequestParam(defaultValue = "") String studentName) {
        /*
         * 思路：
         * 1.查询试卷的批改状态status
         * 2.根据paperId与学生查询学生答案，返回前台批改
         * 3.把分数结果填入表格，在修改批改状态*/
        Page<ExamInfo> page = examInfoService.checkPaper(new Page<>(pageNum, pageSize), status, paperName, studentName);
        return Result.success(page);
    }

    //批改试卷
    @GetMapping("/getAnswer")
    public Result getAnswerByStudentIdAndPaperId(
            @RequestParam(defaultValue = "") String studentId,
            @RequestParam(defaultValue = "") String paperId) {
        //1.获取题目
        //2.根据试卷Id和学号获取答案
        //3.添加分数框
        //4.提交答案
        List<QuestionDTO> list = answerService.getAnswerByStudentIdAndPaperId(paperId, studentId);
//        QueryWrapper<Answer> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("paper_id",paperId);
//        queryWrapper.eq("student_id",studentId);
//        List<Answer> list = answerService.list(queryWrapper);
        return Result.success(list);
    }
}
